/**
 * 
 */
package com.zznode.client.wlanpolling.other;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.zznode.client.wlanpolling.R;
import com.zznode.client.wlanpolling.model.WebSite;
import com.zznode.client.wlanpolling.model.WebSiteManager;

/**
 * @author Pink
 *
 */
public class WebSiteListActivity extends Activity {
	private ListView listView;
	private WebSiteListAdapter listAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.websitelist);
		
		listView = (ListView)findViewById(R.id.listview);
		
		listAdapter = new WebSiteListAdapter(WebSiteManager.getWebSiteManager().getAllWebSite(), this);
		listView.setAdapter(listAdapter);
		
		Button btnAddWebSite = (Button)findViewById(R.id.btnAddWebSite);
		btnAddWebSite.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(WebSiteListActivity.this, WebSiteAddActivity.class);
				WebSiteListActivity.this.startActivityForResult(i, 0);
			}
		});
		
		Button btnDelWebSite = (Button)findViewById(R.id.btnDelWebSite);
		btnDelWebSite.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//删除选中
				for(WebSite webSite : listAdapter.getWebSites()) {
					if(webSite.isSelected()) {
						WebSiteManager.getWebSiteManager().deleteWebSite(webSite.getName());
					}
				}
				listAdapter.setWebSites(WebSiteManager.getWebSiteManager().getAllWebSite());
				listAdapter.notifyDataSetChanged();
			}
		});
		
		listView.setItemsCanFocus(true);
		listView.setClickable(true);
		listView.setCacheColorHint(0);
		listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
		        for(int i=0;i<parent.getCount();i++){
		            View v=parent.getChildAt(parent.getCount()-1-i);
		            if (position == i) {
		                v.setBackgroundColor(Color.GREEN);
		            } else {
		                v.setBackgroundColor(Color.TRANSPARENT);
		            }
		        }
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		listAdapter.setWebSites(WebSiteManager.getWebSiteManager().getAllWebSite());
		listAdapter.notifyDataSetChanged();
		super.onActivityResult(requestCode, resultCode, data);
	}
}

